import {defineStore} from 'pinia';
export const useCounterStore = defineStore('tagData', {
  state: () => {
    return { 
      tags : JSON.parse(sessionStorage.getItem('tag')) || [],
      randomType: ['','success','info','warning','danger']
    }
  },
  // 也可以定义为
  // state: () => ({ count: 0 })
  actions: {
    addTags(val) {
      let num = Math.floor(Math.random()*5);
      //重复不能添加
      let flag =  this.tags.findIndex(item=>item.name==val);
      if(flag==-1){
        this.tags.push({name:val,type:this.randomType[num]});
        sessionStorage.setItem('tag',JSON.stringify(this.tags));
      }
    },
    remoteTags(val){
      this.tags = this.tags.filter(item=>item.name!=val);
      sessionStorage.setItem('tag',JSON.stringify(this.tags))
    }
  },
})